package cn.iocoder.yudao.module.promotion.api.points;

import cn.iocoder.yudao.module.promotion.dal.dataobject.points.PointsActivityDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.points.PointsProductDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.seckill.SeckillActivityDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.seckill.SeckillProductDO;
import cn.iocoder.yudao.module.promotion.dal.mysql.points.PointsProductMapper;
import cn.iocoder.yudao.module.promotion.dal.mysql.seckill.seckillactivity.SeckillProductMapper;
import cn.iocoder.yudao.module.promotion.service.points.PointsActivityService;
import cn.iocoder.yudao.module.promotion.service.points.PointsProductService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

@Service
@Validated
public class PointsApiImpl implements PointsApi{
    @Resource
    private PointsActivityService pointsActivityService;

    @Resource
    private PointsProductService pointsProductService;

    @Resource
    private PointsProductMapper pointsProductMapper;

    @Resource
    private SeckillProductMapper seckillProductMapper;


    @Override
    public void insertParticipationActivity(List<Long> activityId,Integer points,Integer PointsPrice,Long supId) {
        List<PointsActivityDO> pointsActivityList = pointsActivityService.getPointsActivityList(activityId);
        for(PointsActivityDO pointsActivity : pointsActivityList){
            //积分活动添加
            if(pointsActivity != null){
                PointsProductDO productDO = new PointsProductDO();
                productDO.setActivityId(pointsActivity.getId());
                productDO.setActivityStatus((byte) 0);
                productDO.setSpuId(supId);
                productDO.setActivityStartTime(pointsActivity.getStartTime());
                productDO.setActivityEndTime(pointsActivity.getEndTime());
                productDO.setCombinationPrice(PointsPrice);
                productDO.setPoints(points);
                pointsProductMapper.insert(productDO);
            }
        }
        //秒杀活动添加
        List<SeckillActivityDO> seckillActivityList = pointsActivityService.getSeckillActivityListByIds(activityId);
        for(SeckillActivityDO seckillActivityDO : seckillActivityList){
            SeckillProductDO seckillProductDO = new SeckillProductDO();
            seckillProductDO.setActivityId(seckillActivityDO.getId());
            seckillProductDO.setSpuId(supId);
            seckillProductDO.setActivityStatus(0);
            seckillProductDO.setConfigIds(seckillActivityDO.getConfigIds());
            seckillProductDO.setSeckillPrice(PointsPrice);
            seckillProductDO.setActivityStartTime(seckillActivityDO.getStartTime());
            seckillProductDO.setActivityEndTime(seckillActivityDO.getEndTime());
            seckillProductMapper.insert(seckillProductDO);
        }
    }

    @Override
    public  void updateParticipationActivity(List<Long> activityId,Integer points,Integer PointsPrice,Long supId){
        //先清空之前的商品对应的积分活动数据
        pointsProductService.deleteBySpuId(supId);
        List<PointsActivityDO> pointsActivityList = pointsActivityService.getPointsActivityList(activityId);
        for(PointsActivityDO pointsActivity : pointsActivityList){
            PointsProductDO productDO = new PointsProductDO();
            productDO.setActivityId(pointsActivity.getId());
            productDO.setActivityStatus((byte) 0);
            productDO.setSpuId(supId);
            productDO.setActivityStartTime(pointsActivity.getStartTime());
            productDO.setActivityEndTime(pointsActivity.getEndTime());
            productDO.setCombinationPrice(points);
            productDO.setPoints(PointsPrice);
            pointsProductMapper.insert(productDO);
        }
        //清空之前商品对应的秒杀活动数据
        pointsActivityService.deleteBySpuId(supId);
        //秒杀活动添加
        List<SeckillActivityDO> seckillActivityList = pointsActivityService.getSeckillActivityListByIds(activityId);
        for(SeckillActivityDO seckillActivityDO : seckillActivityList){
            SeckillProductDO sp = new SeckillProductDO();
            sp.setActivityId(seckillActivityDO.getId());
            sp.setSpuId(supId);
            SeckillProductDO seckillProductDO = new SeckillProductDO();
            seckillProductDO.setActivityId(seckillActivityDO.getId());
            seckillProductDO.setSpuId(supId);
            seckillProductDO.setActivityStatus(0);
            seckillProductDO.setConfigIds(seckillActivityDO.getConfigIds());
            seckillProductDO.setSeckillPrice(PointsPrice);
            seckillProductDO.setActivityStartTime(seckillActivityDO.getStartTime());
            seckillProductDO.setActivityEndTime(seckillActivityDO.getEndTime());
            seckillProductMapper.insert(seckillProductDO);
        }
    }
}
